GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

report-chart.js ➔ generateBarChart   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
var reportChart = null;
2
3
function formatMoney(number, decimals, decPoint, thousandsSep, currencySymbol)
4
{
5
// *     example: formatMoney(1234.56, 2, ',', ' ');
6
// *     return: '1 234,56'
7
    number = (number + '').replace(',', '').replace(' ', '');
8
    var n = !isFinite(+number) ? 0 : +number,
9
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
10
        sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep,
11
        dec = (typeof decPoint === 'undefined') ? '.' : decPoint,
12
        s = '',
13
        toFixedFix = function (n, prec) {
14
            var k = Math.pow(10, prec);
15
            return '' + Math.round(n * k) / k;
16
        };
17
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
18
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
19
    if (s[0].length > 3) {
20
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
21
    }
22
    if ((s[1] || '').length < prec) {
23
        s[1] = s[1] || '';
24
        s[1] += new Array(prec - s[1].length + 1).join('0');
25
    }
26
    return currencySymbol + s.join(dec);
27
}
28
29
function generateChart(ctx, type, labels, values, options)
30
{
31
    var datasetOptions = $.extend({
32
        data: values
33
    }, options.dataset);
34
35
    var chartOptions = {
36
        type: type,
37
        data: {
38
            labels: labels,
39
            datasets: [datasetOptions]
40
        },
41
        options: {
42
            maintainAspectRatio: false,
43
            legend: {
44
                display: false
45
            },
46
            tooltips: {
47
                callbacks: {
48
                    label: function (tooltipItem, chart) {
49
                        var datasetLabel = chart.datasets[tooltipItem.datasetIndex].label || (chart.labels[tooltipItem.index] || '');
50
                        var datasetValue = chart.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
51
52
                        if (options.isMoneyValue !== undefined && options.isMoneyValue === true) {
53
                            return datasetLabel + ': ' + formatMoney(datasetValue, 2, '.', ',', '$');
54
                        }
55
56
                        return datasetValue;
57
                    }
58
                }
59
            }
60
        }
61
    };
62
63
    if (type !== 'pie' && type !== 'radar' && type !== 'polarArea' && type !== 'doughnut') {
64
        chartOptions.options.scales = {
65
            yAxes: [{
66
                ticks: {
67
                    beginAtZero: true,
68
                    userCallback: function (value, index, values) {
0 ignored issues
show
Unused Code introduced by
The parameter index is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter values is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
69
                        if (options.isMoneyValue !== undefined && options.isMoneyValue === true) {
70
                            return formatMoney(value, 2, '.', ',', '$');
71
                        }
72
73
                        return value;
74
                    }
75
                }
76
            }]
77
        };
78
    }
79
80
    reportChart = new Chart(ctx, chartOptions);
0 ignored issues
show
Bug introduced by
The variable Chart seems to be never declared. If this is a global, consider adding a /** global: Chart */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
81
}
82
83
function generateLineChart(ctx, labels, values, options)
84
{
85
    options = $.extend(options, {
86
        dataset: {
87
            backgroundColor: 'rgba(60,122,167,0.6)',
88
            borderColor: 'rgba(44,79,110,0.95)',
89
            borderWidth: 1
90
        }
91
    });
92
93
    generateChart(ctx, 'line', labels, values, options);
94
}
95
96
function generateBarChart(ctx, labels, values, options)
97
{
98
    options = $.extend(options, {
99
        dataset: {
100
            backgroundColor: 'rgba(60,122,167,0.6)',
101
            borderColor: 'rgba(44,79,110,0.95)',
102
            borderWidth: 1
103
        }
104
    });
105
106
    generateChart(ctx, 'bar', labels, values, options);
107
}
108
109
function generateRadarChart(ctx, labels, values, options)
110
{
111
    options = $.extend(options, {
112
        dataset: {
113
            backgroundColor: 'rgba(60,122,167,0.6)',
114
            borderColor: 'rgba(44,79,110,0.95)',
115
            borderWidth: 1
116
        }
117
    });
118
119
    generateChart(ctx, 'radar', labels, values, options);
120
}
121
122
function generatePolarChart(ctx, labels, values, options)
123
{
124
    options = $.extend(options, {
125
        dataset: {
126
            backgroundColor: 'rgba(60,122,167,0.6)',
127
            borderColor: 'rgba(44,79,110,0.95)',
128
            borderWidth: 1
129
        }
130
    });
131
132
    generateChart(ctx, 'polarArea', labels, values, options);
133
}
134
135
function generatePieChart(ctx, labels, values, options)
136
{
137
    options = $.extend(options, {
138
        dataset: generateRadialData(values)
139
    });
140
141
    generateChart(ctx, 'pie', labels, values, options);
142
}
143
144
function generateDoughnutChart(ctx, labels, values, options)
145
{
146
    options = $.extend(options, {
147
        dataset: generateRadialData(values)
148
    });
149
150
    generateChart(ctx, 'doughnut', labels, values, options);
151
}
152
153
function generateRadialData(values)
154
{
155
    var backgroundColors = ["#1abb9c", "#F7464A", "#46BFBD", "#FDB45C", "#949FB1", "#4D5360", "#bdbdbd", "#AA66CC", "#33B5E5"];
156
    var borderColors = ["#37a37e", "#FF5A5E", "#5AD3D1", "#FFC870", "#A8B3C5", "#616774", "#acacac", "#9933CC", "#0099CC"];
157
    var datasetConfiguration = {
158
        backgroundColor: [],
159
        borderColor: [],
160
        borderWidth: 1
161
    };
162
163
    $.each(values, function(i) {
164
        datasetConfiguration.backgroundColor.push(backgroundColors[i%9]);
165
        datasetConfiguration.borderColor.push(borderColors[i%9]);
166
    });
167
168
    return datasetConfiguration;
169
}
170
171
function initChart($reportCanvas)
172
{
173
    var reportCode = $reportCanvas.attr("id");
174
    var chartType = $reportCanvas.data("type");
175
    var labels = $reportCanvas.data("labels").toString().split(";");
176
    var values = $reportCanvas.data("values").toString().split(";");
177
    var options = $.extend($reportCanvas.data('options'), {
178
        dataset: {}
179
    });
180
181
    var ctx = document.getElementById(reportCode).getContext("2d");
182
183
    switch(chartType) {
184
        case "line": generateLineChart(ctx, labels, values, options); break;
185
        case "bar": generateBarChart(ctx, labels, values, options); break;
186
        case "radar": generateRadarChart(ctx, labels, values, options); break;
187
        case "polar": generatePolarChart(ctx, labels, values, options); break;
188
        case "pie": generatePieChart(ctx, labels, values, options); break;
189
        case "doughnut": generateDoughnutChart(ctx, labels, values, options); break;
190
    }
191
}
192
193
window.addEventListener("load",function()
194
{
195
    $.each($("canvas"), function() {
196
        initChart($(this));
197
    });
198
});
199